home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / emula / arosdv19.lha / AROS / exec / allocpooled.c < prev    next >
C/C++ Source or Header  |  1996-10-24  |  4KB  |  155 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: allocpooled.c,v 1.6 1996/10/24 15:50:44 aros Exp $
  4.     $Log: allocpooled.c,v $
  5.     Revision 1.6  1996/10/24 15:50:44  aros
  6.     Use the official AROS macros over the __AROS versions.
  7.  
  8.     Revision 1.5  1996/10/19 17:07:24  aros
  9.     Include <aros/machine.h> instead of machine.h
  10.  
  11.     Revision 1.4  1996/08/13 13:55:58  digulla
  12.     Replaced AROS_LA by AROS_LHA
  13.     Replaced some AROS_LH*I by AROS_LH*
  14.     Sorted and added includes
  15.  
  16.     Revision 1.3  1996/08/01 17:41:05  digulla
  17.     Added standard header for all files
  18.  
  19.     Desc:
  20.     Lang:
  21. */
  22. #include "exec_intern.h"
  23. #include <aros/libcall.h>
  24. #include <aros/machine.h>
  25. #include "memory.h"
  26.  
  27. /*****************************************************************************
  28.  
  29.     NAME */
  30.     #include <exec/memory.h>
  31.     #include <clib/exec_protos.h>
  32.  
  33.     AROS_LH2(APTR, AllocPooled,
  34.  
  35. /*  SYNOPSIS */
  36.     AROS_LHA(APTR,  poolHeader, A0),
  37.     AROS_LHA(ULONG, memSize,    D0),
  38.  
  39. /*  LOCATION */
  40.     struct ExecBase *, SysBase, 118, Exec)
  41.  
  42. /*  FUNCTION
  43.     Allocate memory out of a private memory pool.
  44.  
  45.     INPUTS
  46.     poolHeader - Handle of the memory pool
  47.     memSize    - Number of bytes you want to get
  48.  
  49.     RESULT
  50.     A pointer to the number of bytes you wanted or NULL if the memory
  51.     couldn't be allocated
  52.  
  53.     NOTES
  54.  
  55.     EXAMPLE
  56.  
  57.     BUGS
  58.  
  59.     SEE ALSO
  60.     CreatePool(), DeletePool(), FreePooled()
  61.  
  62.     INTERNALS
  63.  
  64.     HISTORY
  65.     16-10-95    created by M. Fleischer
  66.  
  67. ******************************************************************************/
  68. {
  69.     AROS_LIBFUNC_INIT
  70.  
  71.     APTR ret;
  72.     struct Pool *pool=(struct Pool *)poolHeader;
  73.  
  74.     /* If the memSize is bigger than the ThreshSize allocate seperately. */
  75.     if(memSize>pool->ThreshSize)
  76.     {
  77.     ULONG size;
  78.     struct Block *bl;
  79.  
  80.     /* Get enough memory for the memory block including the header. */
  81.     size=memSize+BLOCK_TOTAL;
  82.     bl=(struct Block *)AllocMem(size,pool->Requirements);
  83.  
  84.     /* No memory left */
  85.     if(bl==NULL)
  86.         return NULL;
  87.  
  88.     /* Initialize the header */
  89.     bl->Size=size;
  90.  
  91.     /* Add the block to the BlockList */
  92.     AddHead((struct List *)&pool->BlockList,(struct Node *)&bl->Node);
  93.  
  94.     /* Set pointer to allocated memory */
  95.     ret=(UBYTE *)bl+BLOCK_TOTAL;
  96.     }else
  97.     {
  98.     struct MemHeader *mh;
  99.  
  100.     /* Follow the list of MemHeaders */
  101.     mh=(struct MemHeader *)pool->PuddleList.mlh_Head;
  102.     for(;;)
  103.     {
  104.         /* Are there no more MemHeaders? */
  105.         if(mh->mh_Node.ln_Succ==NULL)
  106.         {
  107.         /* Get a new one */
  108.         mh=(struct MemHeader *)
  109.            AllocMem(pool->PuddleSize+MEMHEADER_TOTAL,pool->Requirements);
  110.  
  111.         /* No memory left? */
  112.         if(mh==NULL)
  113.             return NULL;
  114.  
  115.         /* Initialize new MemHeader */
  116.         mh->mh_First=(struct MemChunk *)((UBYTE *)mh+MEMHEADER_TOTAL);
  117.         mh->mh_First->mc_Next=NULL;
  118.         mh->mh_First->mc_Bytes=pool->PuddleSize;
  119.         mh->mh_Lower=mh->mh_First;
  120.         mh->mh_Upper=(UBYTE *)mh->mh_First+pool->PuddleSize;
  121.         mh->mh_Free=pool->PuddleSize;
  122.  
  123.         /* And add it to the list */
  124.         AddHead((struct List *)&pool->PuddleList,(struct Node *)&mh->mh_Node);
  125.         /* Fall through to get the memory */
  126.         }
  127.         /* Try to get the memory */
  128.         ret=Allocate(mh,memSize);
  129.  
  130.         /* Got it? */
  131.         if(ret!=NULL)
  132.         break;
  133.  
  134.         /* No. Try next MemHeader */
  135.         mh=(struct MemHeader *)mh->mh_Node.ln_Succ;
  136.     }
  137.     /* Allocate does not clear the memory! */
  138.     if(pool->Requirements&MEMF_CLEAR)
  139.     {
  140.         ULONG *p=(ULONG *)ret;
  141.  
  142.         /* Round up (clearing longs is faster than just bytes) */
  143.         memSize=(memSize+sizeof(ULONG)-1)/sizeof(ULONG);
  144.  
  145.         /* NUL the memory out */
  146.         while(memSize--)
  147.         *p++=0;
  148.     }
  149.     }
  150.     /* Everything fine */
  151.     return ret;
  152.     AROS_LIBFUNC_EXIT
  153. } /* AllocPooled */
  154.  
  155.